package com.easytopit.demo.spring.mvc.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.easytopit.demo.spring.mvc.domain.ValidUser;

@Controller
@RequestMapping("/valid")
public class ValidController {

	@GetMapping("/index")
	public String index(@ModelAttribute("user") ValidUser testObject) {
		return "valid/valid";
	}

	@PostMapping("/post")
	public String post(@Valid @ModelAttribute("user") ValidUser testObject, BindingResult result) {
		if (!result.hasErrors()) {
			System.out.println("验证成功");
			return "valid/ok";
		}
		return "valid/valid";
	}

}
